package com.jumi.microservice.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumi.microservice.jwt.annon.IgnoreUrlsAnnon;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.Article;
import com.jumi.microservice.domain.ArticleClass;
import com.jumi.microservice.domain.dto.ArticleDTO;
import com.jumi.microservice.domain.vo.ArticleVO;
import com.jumi.microservice.mapper.ArticleClassMapper;
import com.jumi.microservice.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/ArticleApi")
@Api(tags = "C-文章管理相关接口")
public class ArticleApiController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleClassMapper articleClassMapper;

    @Value("${articleClass.helpId}")
    private Long helpArticleClassId;

    @ApiOperation(value = "根据文章id获取文章详情接口",notes = "根据文章id获取文章详情接口")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/AppArticleDetail")
    @IgnoreUrlsAnnon("/AppArticleDetail")
    public ResponseResult<ArticleVO> AppArticleDetail(Long id){
        ArticleDTO articleDTO = articleService.selectId(id);
        ArticleClass articleClass = articleClassMapper.selectById(articleDTO.getAcid());
        articleDTO.setAcName(articleClass.getAcName());
        return ResultGenerator.genSuccessResult(dto2Vo(articleDTO));
    }

    @ApiOperation(value = "根据类型id获取文章详情接口",notes = "根据类型id获取文章详情接口")
    @ApiImplicitParam(name = "acid", value = "acid")
    @GetMapping("/AppArticleList")
    @IgnoreUrlsAnnon("/AppArticleList")
    public ResponseResult<List<ArticleVO>> AppArticleList(Long acid){
        ArticleClass articleClass = articleClassMapper.selectById(acid);

        LambdaQueryWrapper<Article> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Article::getAcid,acid);
        wrapper.eq(Article::getIsDel,0);
        wrapper.orderByDesc(Article::getId);
        List<Article> list = articleService.list(wrapper);

        for (Article article : list){
            article.setAcName(articleClass.getAcName());
        }
        return ResultGenerator.genSuccessResult(pos2Vos(list));
    }

    /**
     * 获取文章分类“帮助中心”下的所有二级分类信息
     */
    @ApiOperation(value = "获取“帮助中心”下的所有二级分类信息",notes = "获取“帮助中心”下的所有二级分类信息")
    @GetMapping("/AppArticleClassHelpList")
    @IgnoreUrlsAnnon("/AppArticleClassHelpList")
    public ResponseResult<List<ArticleClass>> AppArticleClassHelpList(){
        List<ArticleClass> articleClass = articleClassMapper.selectList(
                Wrappers.<ArticleClass>lambdaQuery()
                        .eq(ArticleClass::getAcParentId, helpArticleClassId)
                        .eq(ArticleClass::getIsDel, 0)
        );
        return ResultGenerator.genSuccessResult(articleClass);
    }


    /**
     * 部门对象转换 dto->vo
     *
     * @param dto 部门dto
     * @return 部门vo
     */
    private ArticleVO dto2Vo(ArticleDTO dto) {
        ArticleVO vo = null;
        try {
            vo = dto.clone(ArticleVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return vo;
    }

    /**
     * 部门DO对象转换 do->vo
     *
     * @param articles 部门do集合
     * @return 部门VO集合
     */
    private List<ArticleVO> pos2Vos(List<Article> articles) {
        List<ArticleVO> pos = articles.stream().map(p -> {
            ArticleVO vo = null;
            try {
                vo = p.clone(ArticleVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }
}
